package android.textIt.frontend.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.textIt.R;
import android.textIt.backend.model.User;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;

public class GroupMemberAdapter extends ArrayAdapter<User> {
	 private Context context;
	 private List<User> items;
	 private List<User> members;
	 private int resourceId;
	 
   public GroupMemberAdapter(Context context, int textViewResourceId, List<User> userList) {
	     super(context, textViewResourceId, userList);
	     this.context = context;
	     this.items = userList;
	     this.members = new ArrayList<User>();
	     this.resourceId = textViewResourceId;
	}
	 
   public GroupMemberAdapter(Context context, int textViewResourceId, List<User> userList, List<User> memberList) {
       super(context, textViewResourceId, userList);
       this.context = context;
       this.items = userList;
       this.members = memberList;
	     this.resourceId = textViewResourceId;
   }
   
   @Override
   public View getView(final int position, View convertView, ViewGroup parent) {
       if (convertView == null) {
           LayoutInflater vi = 
          	 (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
           convertView = vi.inflate(resourceId, null);
       }
       User user = items.get(position);
       
       CheckBox chkMember = (CheckBox) convertView.findViewById(R.id.chkPotentialMember);
       chkMember.setText(" " + user.getName());
       
       if (isAlreadyMember(user)){
      	 chkMember.setChecked(true);
       }

       return convertView;
   }
   
   private boolean isAlreadyMember(User u){
  	 for (User member: members){
  		 if (member.getId() == u.getId()){
  			 return true;
  		 }
  	 }
  	 return false;
   }
}
